using System;
using Prologis.Logging;

namespace Prologis.ShifterNT.Tokenizer
{


    public class RLTokenizer : ITokenizer
    {


        Logger logger = new Logger("Prologis.Shifter");
        public string FieldConfigPath { get; set; }

        FieldList fields;
        public FieldList Fields
        {
            get { return fields; }
            set { fields = value; }
        }



        string ignoreString = null;
        public string IgnoreString
        {
            get { return ignoreString; }
            set { ignoreString = value; }
        }


        bool verbose = false;
        public bool Verbose
        {
            get { return verbose; }
            set { verbose = value; }
        }



        public FieldList Tokenize(string sourceId, object data)
        {

            FieldList fieldList = (FieldList)fields.Clone();
            string line = data.ToString();

            fieldList.HasValues = false;


            foreach (Field field in fieldList)
            {


            }

            foreach (Field field in fieldList)
            {

                if (field.Source == sourceId)
                {

                    if (field.Length == 0 && field.Pos == 0)
                    {
                        field.Value = line;
                    }
                    else
                    {
                        try
                        {
                            string name = field.Name;
                            string value = line.Substring(field.Pos, field.Length).Trim();
                            field.Value = value;
                            if (Verbose)
                            {
                                logger.Verbose("Name=[" + name + "] Value=[" + value + "]");
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception("Tokenizing failed at \r\nLine=[" + line + "] \r\nLineLength=[" + line.Length + "] \r\nField=[" + field.Name + "] ", e);
                        }
                    }
                }
            }
            fieldList.HasValues = true;


            return fieldList;
        }
    }
}
